package com.neu.wms.security;

import com.neu.wms.entity.SysUser;
import com.neu.wms.vo.MenuVo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author DAIJUNQIANG
 * @version 1.0
 * @description 用于封装用户信息
 * @date 2023/7/3 22:19
 */

@Data
public class LoginUser implements UserDetails {
	private SysUser user;
	private List<MenuVo> menuVoList;
	private String[] permissions;//得当前用户拥有的权限
	private String[] roles;//得到当前用户拥有的角色
	private String token;

	//授权
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		ArrayList<GrantedAuthority> authorities = new ArrayList<>();
		if (permissions != null) {
			for (String permission : permissions) {
				authorities.add(new SimpleGrantedAuthority(permission));
			}
		}
		if (roles != null) {
			for (String role : roles) {
				//加ROLE_前缀是为了和permission进行区分
				authorities.add(new SimpleGrantedAuthority("ROLE_"+role));
			}
		}
		return authorities;
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUserName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
